$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var stove = "";

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        $.confirm("确定返回吗？", function () {
            localStorage.removeItem("work");
            window.location.href = "acidTransfer.html";
        }, function () {
            $("#goods_id").focus();
        });
    });

    //点击 显示 隐藏
    $(".head .icon-xianshi").on("click", function () {
        $(this).hide();
        $(".head .icon-yincang").show();
        $(".details").show();
        //$alert("显示成功");
        $("#goods_id").val("").focus();
    });
    $(".head .icon-yincang").on("click", function () {
        $(this).hide();
        $(".head .icon-xianshi").show();
        $(".details").hide();
        //$alert("隐藏成功");
        $("#goods_id").val("").focus();
    });

    var work = JSON.parse(localStorage.getItem("work"));
    $(".details span.good_ydh").html(work.driver_qrcode_bill_no);
    $(".details span.good_pm").html(work.productName);
    $(".details span.good_gg").html(work.productSpecification);
    $(".details span.good_zl").html(work.planWeight);
    $(".details span.good_js").html(work.driver_qrcode_count);
    /*var locationHtml = '';
    for (var i = 0; i < work.locationInventorys.length; i++){
        locationHtml += '<p>指引库位:<span class="good_location">'+
            work.locationInventorys[i]._id.location +'</span>件数:<span class="good_locationLength">'+
            work.locationInventorys[i].lengths +'</span>重量:<span class="good_locationWeight">'+
            work.locationInventorys[i].weights +'</span></p>'
    }
    $(".details").prepend(locationHtml);*/

    //搜索框 获取焦点
    $("#goods_id").focus();
    //根据扫描内容 得到 酸洗物品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描酸洗物品标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            if ($(".content .list-block .goodsList_ul li[attr-data='" + val + "']").length != 0) {
                $alert("不可重复扫描");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            if ($(".content .list-block .goodsList_ul li").length == work.driver_qrcode_count) {
                $alert("已扫描" + work.driver_qrcode_count + "件了，请点击确认");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + "/app/acidTransfer/getAcidwarehouse",
                data: {
                    _id: work.id,
                    batch: val
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if ($(".content .list-block .goodsList_ul li[attr-data='" + data.goods_no + "']").length != 0) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        } else {
                            if (data.stove) stove = data.stove;
                            var data_html = '<li attr-data="' + data.goods_no + '"><div class="row no-gutter">' +
                                '<div class="col-90">' +
                                '<p class="p_none"><span class="sp goods_id">' + data._id + '</span></p>' +
                                '<p><span>编号:</span><span class="sp goods_number">' +
                                data.goods_no + '</span></p><p><span>重量:</span><span class="sp goods_cpjz">' +
                                (data.weight_of_wire_rod ? data.weight_of_wire_rod : data.goods_weight_use) + '</span>T</p></div>' +
                                '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                            $(".content .list-block .goodsList_ul").prepend(data_html);
                            badge_text();
                            $alert(data.goods_no + "获取成功", 800);
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                        }
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().remove();
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    //点击 清空数据
    $(".foot").on("click", ".add_go", function () {
        $.confirm("确认清空数据吗？", function () {
            $(".content .list-block .goodsList_ul").empty();
            $alert("清空成功");
            badge_text();
            setTimeout(function () {
                $("#goods_id").val("").focus();
            }, 1400);
        })
    });

    // 点击 提交 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请至少扫描一个酸洗物品！");
        }
        if ($(".content .goodsList_ul li").length != work.driver_qrcode_count) {
            $("#goods_id").val("").focus();
            return $alert("件数不匹配，请继续扫描！");
        }
        $.confirm('确定调拨' + $(".content .goodsList_ul li").length + '件？', function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var numbers = [];
            var ids = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                numbers.push($(".content .goodsList_ul li").eq(i).find("span.goods_number").text());
                ids.push($(".content .goodsList_ul li").eq(i).find("span.goods_id").text());
            }
            loading_show("提交中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/acidTransfer/saveAcidTransferorder",
                data: {
                    _id: work.id,
                    bill_no: work.driver_qrcode_bill_no,
                    ids: JSON.stringify(ids),
                    batchs: JSON.stringify(numbers)
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        console.log("酸洗调拨成功");
                        localStorage.removeItem("work");
                        window.location.href = "acidTransfer.html";
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    if (error.status == 0) {
                        pass = 1;
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpjz").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();

});